package com.zhiyou.homework.controller;

import com.zhiyou.common.pojo.Result;
import com.zhiyou.common.pojo.StateCode;
import com.zhiyou.homework.pojo.*;
import com.zhiyou.homework.service.HomeworkService;
import com.zhiyou.homework.service.ParentService;
import com.zhiyou.homework.service.StudentHomeworkService;
import com.zhiyou.homework.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RestController
@RequestMapping("/homework")
public class HomeworkController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private HomeworkService homeworkService;

    @Autowired
    private ParentService parentService;

    @Autowired
    private StudentHomeworkService stuHwService;

    /**
     * 发布作业 图片+视频
     * 接口测试OK
     * TODO 暂时上传至当前工程根目录下
     */
    @PostMapping("/release/upload")
    public Result releaseHomeworkImgOrVideo(@PathVariable("file") MultipartFile file) {
        return teacherService.releaseHomeworkImgOrVideo(file);
    }


    /**
     * 教师作业发布
     * @param tid  老师id
     * @param content 发布作业内容
     * @param url  发布作业视频或图片地址
     * 暂定表单提交
     * 接口测试OK
     * TODO 作业内容和url是以form表单发送? json格式发送?
     */
    @PostMapping("/release/{tid}")
    public Result releaseHomework(@PathVariable Integer tid,
                                  String content,
                                  @RequestParam(required = false,defaultValue = "") String url) {
        homeworkService.insertHomework(tid,content,url);
        return new Result(true, StateCode.OK,"作业发布成功");
    }

    /**
     * 作业管理界面需要展现老师所教班级信息下拉框
     * 以及查询所教第一个班级学生作业完成情况
     * @param tid 教师id
     * @param gradeId 班级id
     * @param statusCode  状态码 1通过 2未通过 3待审批
     * @param releaseTime 作业发布日期
     * 接口测试OK
     */
    @GetMapping({"/findAll/{tid}","/findAll/{tid}/{gradeId}/{statusCode}/{releaseTime}"})
    public Result findAllByTeacherId(@PathVariable Integer tid,
                                     @PathVariable(required = false) Integer gradeId,
                                     @PathVariable(required = false) Integer statusCode,
                                     @PathVariable(required = false) String releaseTime) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("tid",tid);
        paramMap.put("gradeId",gradeId);
        paramMap.put("statusCode",statusCode);
        paramMap.put("releaseTime",releaseTime);
        List<StudentExt> studentExtList = null;
        // 查询老师所教所有班级及科目,且按班级编号降序
        Map<String, TreeSet> gradeCourseMap = teacherService.findGradeAndCourseByTeacherId(tid);
        // 首次打开作业管理,根据教师id查询全部班级,所教课程,即第一个班级所有学生作业
        TreeSet gradeSet = gradeCourseMap.get("grade");
        if (gradeId == null && statusCode == null && releaseTime == null) {
            Grade firstGrade = (Grade)gradeSet.first();
            paramMap.put("gradeId",firstGrade.getGid());
            // 根据班级id 返回学生姓名及作用完成状态
            studentExtList = teacherService.findStuNameAndStatusByGid(paramMap);
        }else { // 根据班级id+状态+作业法发布时间 查询学生作业
            // 根据班级id 返回学生姓名及作用完成状态
            studentExtList = teacherService.findStuNameAndStatusByGid(paramMap);
        }

        Map<String,Collection> map = new HashMap<>();
        map.put("grade",gradeSet);
        map.put("course",gradeCourseMap.get("course"));
        map.put("student",studentExtList);
        return new Result(true, StateCode.OK,"查询所有已提交作业学生作业成功",map);
    }

    /**
     * 查询单个学生作业详情
     * @param sid 学生id
     * @param tid 教师id
     * 接口测试OK
     */

    @GetMapping("/findOne/{sid}/{tid}")
    public Result findOneHwInfoBySid(@PathVariable Integer sid,@PathVariable Integer tid) {
        StudentHomeworkExt studentHomeworkExt = teacherService.findOneHwInfoBySid(sid,tid);
        return new Result(true, StateCode.OK,"查询单个学生作业成功",studentHomeworkExt);
    }

    /**
     * 教师审批作业
     * @param tid 教师id
     * @param sid 学生id
     * @param statusCode 状态码
     * @param subTime 作业提交布时间
     * 接口测试OK
     */
    @PutMapping("/check/{sid}/{tid}/{statusCode}/{subTime}")
    public Result checkHomework(@PathVariable Integer sid,
                                @PathVariable Integer tid,
                                @PathVariable Integer statusCode,
                                @PathVariable String subTime) {
        boolean result = teacherService.checkHomework(sid,tid,statusCode,subTime);
        if (result) {
            return new Result(true, StateCode.OK,"审批学生作业成功");
        }
        return new Result(false, StateCode.ERROR,"审批学生作业失败");

    }

    /**
     * 家长端查看全部作业
     * @param pid 家长id
     * @param releaseTime 发布作业时间
     */
//    @GetMapping("/parent/{pid}/{releaseTime}")
//    public Result parentFindAllHw(@PathVariable Integer pid,
//                                @PathVariable String releaseTime) {
//
//        List<Student> students = parentService.findAllStu(pid);
//
//        Map<String,Object> map = stuHwService.findAllHw(students);
//        return new Result(true, StateCode.OK,"查询孩子学生全部科目作业",map);
//
//    }

}
